﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class wjTrade : System.Web.UI.Page
{
    string  ID = "", Menu_ID = "";
    public string Title;
    public string Keywords;
    public string Description;
    string strSQL, SQL;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            WebSiteInfo();
            pro_sm();
            list_bind();
        }
    }
    public void WebSiteInfo()
    {
        DataTable dt = Class1.ExecSel("select WS_Title,WS_keywords,WS_description from Site_Config");
        this.Title = Convert.ToString(dt.Rows[0]["WS_Title"]);
        this.Keywords = Convert.ToString(dt.Rows[0]["WS_keywords"]);
        this.Description = Convert.ToString(dt.Rows[0]["WS_description"]);
    }
    public void pro_sm()
    {
        
        if (Request.QueryString["Menu_ID"] != null)
        {
            int ID = int.Parse(Request.QueryString["Menu_ID"]);
            SQL = "select Menu_ParentLevel,Menu_ID,Menu_Name from Class where Menu_ParentLevel=" + ID;
            DataTable dt = Class1.ExecSel(SQL);
            dl_sj.DataSource = dt;
            dl_sj.DataKeyField = "Menu_ID";
            dl_sj.DataBind();

            SqlConnection con = Class1.DBconnection();
            string sqlStr = "select p_z_ID from Product where p_z_ID=" + ID + "";
            SqlDataAdapter da = new SqlDataAdapter(sqlStr, con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Product");
            string num = ds.Tables["Product"].Rows.Count.ToString();
            e_Count.Text = num;
            da.Dispose();
            ds.Dispose();
            con.Close();
            con.Dispose();
        }
    }
    protected void dl_sj_DataBinding(object sender, DataListItemEventArgs e)
    {
        Label Label1 = (Label)e.Item.FindControl("s_Count");
        SqlConnection con = Class1.DBconnection();
        string sqlStr = "select p_s_ID from Product where p_s_ID=" + this.dl_sj.DataKeys[e.Item.ItemIndex].ToString() + "";
        SqlDataAdapter da = new SqlDataAdapter(sqlStr, con);
        DataSet ds = new DataSet();
        da.Fill(ds, "Product");
        string num = ds.Tables["Product"].Rows.Count.ToString();
        Label1.Text = num;
        da.Dispose();
        ds.Dispose();
        con.Close();
        con.Dispose();
    }

    private void list_bind()
    {
       
        SqlConnection conn = Class1.DBconnection();

        int Menu_ID = int.Parse(Request.QueryString["Menu_ID"]);
        if (Menu_ID != null)
        {
            SQL = "select * from Product where p_z_ID=" + Menu_ID + " order by p_ID desc";
        }
        else
        {
            SQL = "select * from Product order by p_ID desc";
        }

        SqlDataAdapter da = new SqlDataAdapter(SQL, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataTable dt = ds.Tables[0];

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (ds.Tables[0].Rows[i]["p_Title"].ToString().Length > 32)
            {
                ds.Tables[0].Rows[i]["p_Title"] = ds.Tables[0].Rows[i]["p_Title"].ToString().Substring(0, 32) + "...";
            }
            if (ds.Tables[0].Rows[i]["p_Content"].ToString().Length > 100)
            {
                string str = ds.Tables[0].Rows[i]["p_Content"].ToString();
                str = str.Replace("<BR>", "");
                str = str.Replace("&nbsp;", "");

                ds.Tables[0].Rows[i]["p_Content"] = str.Substring(0, 100) + "...";
            }
        }


        if (dt.Rows.Count > 0)
        {
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = ds.Tables[0].DefaultView;

            pds.AllowPaging = true;
            pds.PageSize = 10;
            int curPage;

            if (Request.QueryString["page"] != null)
            {
                curPage = Convert.ToInt32(Request.QueryString["page"]);
            }
            else
            {
                curPage = 1;
            }

            pds.CurrentPageIndex = curPage - 1;
            this.Repeater1.DataSource = pds;
            this.Repeater1.DataBind();
            this.Label1.Text += "<a>共有<font color=red><b>" + ds.Tables[0].Rows.Count.ToString() + "</b></font>条记录</a>&nbsp;";
            this.Label1.Text += "<a>每页显示<font color=red><b>" + pds.PageSize + "</b></font>条</a>&nbsp;";
            this.Label1.Text += "<a>共<font color=red><b>" + pds.PageCount.ToString() + "</b></font>页</a>&nbsp;";
            this.Label1.Text += "<a>当前第<font color=red><b>" + curPage.ToString() + "</b></font>页</a>&nbsp;&nbsp;";
            if (curPage == 1)
            {
                this.Label1.Text += "<a>首页</a>&nbsp;";
                this.Label1.Text += "<a>上一页</a>&nbsp;";
            }
            else
            {
                this.Label1.Text += "<a href='wjTrade-1-" + Menu_ID + ".html'>首页</a>&nbsp;";
                this.Label1.Text += "<a href='wjTrade-" + (curPage - 1).ToString() + "-" + Menu_ID + ".html'>上一页</a>&nbsp;&nbsp;";
            }
            if (curPage == pds.PageCount)
            {
                this.Label1.Text += "<a>下一页</a>&nbsp;";
                this.Label1.Text += "<a>尾页</a>&nbsp;";
            }
            else
            {
                this.Label1.Text += "<a href='wjTrade-" + (curPage + 1).ToString() + "-" + Menu_ID + ".html'>下一页</a>&nbsp;";
                this.Label1.Text += "<a href='wjTrade-" + pds.PageCount.ToString() + "-" + Menu_ID + ".html'>尾页</a>&nbsp;";
            }

            string HtmlSelectString = "<select onchange=\"javascript:window.location=this.value\">";
            for (int i = 1; i <= pds.PageCount; i++)
            {
                if (i == curPage)
                {
                    HtmlSelectString += "<option value=wjTrade-" + i + "-" + Menu_ID + ".html selected>第" + i + "页</option>";
                }
                else
                {
                    HtmlSelectString += "<option value=wjTrade-" + i + "-" + Menu_ID + ".html>第" + i + "页</option>";
                }
            }
            HtmlSelectString += "</select>";

            this.Label1.Text += "<a>" + HtmlSelectString + "</a>";
        }
        else
        {
            this.Label1.Text = "当前没有记录";
        }
        ds.Dispose();
        da.Dispose();
        conn.Close();
        conn.Dispose();
    }
}
